from sets import Set

def solver(courses):
    """Function which accept dictionary of courses and return a list of courses' names where no two courses' hours collide and no two courses' test are on the same date
    courses - dictionary of name : stat
        name - course name
        stat - a tuple (hours,MoedA,MoedB)
            hours - a tuple with course hours (107 means 7th hour in the first day)
            MoedA & MoedB - test date (909 means September 9th) """
    
    retCourses = []
    chosenHours = Set([])
    
    for (course, stats) in courses.items():
        cantChoose = 0
        for hour in stats[0]:
            if hour in chosenHours:
                cantChoose = 1
        if course[-1].isdigit() and course[-1] in [elem[-1] for elem in retCourses]:
                cantChoose = 1
        if cantChoose == 0:
            for hour in stats[0]:
                chosenHours.add(hour)
            retCourses.append(course)
    
    testDates = []
    for course in retCourses:
        if (courses[course])[1]  in testDates or (courses[course])[2] in testDates:
            retCourses.remove(course)
        else:
            testDates.append((courses[course])[1])
            testDates.append((courses[course])[2])
    return retCourses
                


if __name__ == "__main__":
    print solver({"sec":((101,102,103),710,812) , "numeric" :((107,108,109,110),712,901) , "number":((307,308,509,510),705,816) ,"pixar":((509,510,511),707,910) ,"ebay":((406,407,408),717,929) ,"talks":((207,208,209),712,915) ,"cpp":((409,410,411,412),718,829) ,"db":((306,307,308),702,902) ,"complex1":((303,304,305,308),705,804) ,
          "complex2":((303,304,305,309),705,804) ,"complex3":((303,304,305,311),705,804) ,"logic1":((102,103,104,401),701,811) ,"logic2":((102,103,104,402),701,811) ,"algo1":((309,310,311,504),710,909) ,"algo2":((309,310,311,503),710,909) ,"algo3":((105,106,107,506),710,909) ,"algo4":((105,106,107,510),710,909) ,"image":((303,304,305),628,728)})

    